<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";

%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="">
    <meta name="author" content="jie">
    <title>渠道打包</title>
    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css"
          rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="css/grid.css"
          rel="stylesheet">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/jquery.qrcode.js"></script>
    <script type="text/javascript" src="js/qrcode.js"></script>
    <script type="text/javascript">
        var basePath = "<%=basePath %>";
        var status_map = {0: "待处理", 1: "正在打包", 2: "完成", 3: "失败"}

        function queryDBLog() {
            $.ajax({
                url: basePath + "query",
                dataType: 'json',
                success: function (response) {
                    if (null != response.data) {
                        var htmlStr = "";
                        for (i  in  response.data) {
                            var channel = response.data[i];
                            htmlStr += "<tr>";
                            htmlStr += "<td>" + channel.channel_name + "</td>";
                            htmlStr += "<td>" + channel.version + "</td>";
                            htmlStr += "<td>" + channel.create_date + "</td>";
                            htmlStr += "<td>" + status_map[channel.status] + "</td>";
                            if (2 == channel.status) {
                                var apkName = "NOCARD.apk";
                                htmlStr += "<td><a href='" + apkName + "'>" + apkName + "</a></td>";
                            }
                            else {
                                htmlStr += "<td></td>";
                            }
                            htmlStr += "</tr>";
                        }
                        $("#content_tbody").html(htmlStr);

                    }
                }
            });
        }

        function autoQuery() {
            queryDBLog();
            updateGitLog();
            queryTag();
            setTimeout("autoQuery()", 60000);
        }

        function queryTag() {
            $.ajax({
                url: basePath + "queryTag",
                data: {},
                dataType: 'json',
                success: function (response) {
                    if (0 == response.ret_code) {
                        alert("失败");
                    }
                    else {
                        $("#tag_list").empty();
                        $("#tag_list").append(response.ret_msg);
                    }
                    $("#submit_loading").hide();
                    $("#submitButton").show();

                }
            });
        }

        function submitChannel() {
            $("#submit_loading").show();
            $("#submitButton").hide();
            $.ajax({
                url: basePath + "submit",
                data: {tag: $("#tag_list").val()},
                dataType: 'json',
                success: function (response) {

                    if (0 == response.ret_code) {
//                        alert(response.ret_msg);
                    }
                    else {
                        queryDBLog();
                    }

                    $("#submit_loading").hide();
                    $("#submitButton").show();

                }
            });
        }

        function updateGitLog(reqCode) {
            $.ajax({
                url: basePath + "updateGitLog",
                data: {"reqCode": reqCode},
                dataType: 'json',
                success: function (response) {
                    if (0 == response.ret_code)//无更新
                    {
                        $('#qr_code_release').show();
                        $('#qr_code_loading').hide();
                        $("#what_new").html(response.ret_msg);
                    }
                    else if (1 == response.ret_code)//打包中
                    {
                        $('#qr_code_release').hide();
                        $('#qr_code_loading').show();
                    }
                    else {
                        alert("打包失败");
                        $('#qr_code_release').show();
                        $('#qr_code_loading').hide();
                        $("#what_new").html(response.ret_error_msg);
                    }
                }
            });
        }

        function queryQRCode() {
            $('#qr_code_release').qrcode({
                width: 128,
                height: 128,
                text: basePath + "NOCARD.apk"
            });
        }
    </script>
</head>
<body onload="queryQRCode();autoQuery();">
<div class="container">

    <div class="page-header">
        <h1>手付通打包系统</h1>
        <p class="lead">目前提供自动打渠道包、下载及记录查看，自动邮件推送暂未开发！</p>
    </div>

    <div class="row">
        <div class="container">
            <!-- Example row of columns -->
            <div class="row">
                <div class="col-md-3" style="height:380px;">
                    <form class="navbar-form navbar-left" role="search">
                        <h4 class="form-signin-heading">输入渠道包名称 :</h4>
                        <div class="form-group">
                            <select id="tag_list" class="form-control">

                            </select>
                        </div>
                        <button id="submitButton" onclick="submitChannel();" type="button" class="btn btn-success">打包
                        </button>
                        <img id="submit_loading" hidden="true" alt="" width="50" height="30" src="image/loading2.gif"/>
                    </form>
                </div>
                <div class="col-md-7" style="height:380px;overflow-y:scroll;">
                    <h5 style="color:#208e48;">日志</h5>
                    <div id="what_new">


                    </div>
                </div>
                <div class="col-md-2" style="height:380px;">
                    <h5 style="color:#208e48;">正式接口</h5>
                    <img id="qr_code_loading" alt="" width="128" height="128" src="image/loading.gif"/>
                    <div id="qr_code_release" hidden="true"
                         onclick="window.location.href='NOCARD.apk'"></div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="container" id="content">
            <h2 class="sub-header">日志</h2>
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>渠道名</th>
                        <th>打包版本号</th>
                        <th>提交日期</th>
                        <th>状态</th>
                        <th>下载</th>
                    </tr>
                    </thead>
                    <tbody id="content_tbody">

                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</body>
</html>

